    $(document).ready(function() {
    	var whereClause={"all":"all"};
    	$.ajax({
    		url:'http://localhost:8080/easyskool/rest/moderatorservice/getModeratorDetails',
    		type:'POST',
    		data: '{"user":"sumit","whereClause":'+null+'}',
    		dataType: 'json',
    		contentType: "application/json; charset=utf-8",
    		success:function (data,testStatus,response) {
    			var jsondata =JSON.parse(response.responseText);
				
    			$.each(jsondata, function(i, item) {
    			$("#mytable").append("<tr id = " + i + ">" +
    								 "<td><input type='checkbox' name='checkboxselect' title='Select All' id="+i+" /></td>"+
    								 "<td class='aaCode'>" 			+ item.aaCode					+ "</td>" +
			    					 "<td class='mdrFname'>" 		+ item.mdrFname 				+ "</td>" +
			    					 "<td class='mdrNmane'>"		+ item.mdrNmane 				+ "</td>" +
			    					 "<td class='mdrLname'>" 		+ item.mdrLname 				+ "</td>" +
			    					 "<td class='mdrAddress'>" 		+ item.mdrAddress 				+ "</td>" +
			    					 "<td class='mdrCity'>" 		+ item.mdrCity					+ "</td>" +
			    					 "<td class='mdrState'>" 		+ item.mdrState					+ "</td>" +
			    					 "<td class='mdrCountry'>" 		+ item.mdrCountry				+ "</td>" +
			    					 "<td class='mdrPin'>" 			+ item.mdrPin					+ "</td>" +
			    					 "<td class='mdrEmail'>" 		+ item.mdrEmail					+ "</td>" +
			    					 "<td class='mdrCurrentSchoolCode'>" + item.mdrCurrentSchoolCode	+ "</td>" +
			    					 "<td class='createdBy'>" 		   + item.createdBy		+ "</td>" +
			    					 "<td class='createdDate'>" 	   + item.createdDate	+ "</td>" +
			    					 "<td class='modifiedBy'>" 		   + item.modifiedBy	+ "</td>" +
			    					 "<td class='modifiedDate'>" 	   + item.modifiedDate	+ "</td>" +
			    					 "<td><input type='button' value='Update' class='btn btn-info' id='update' data-toggle='modal' data-target='#myUpdateModal' /></td>" +
			    					 "<td><input type='button' value='Delete' class='btn btn-danger' id='remove'/></td></tr>");
 
    			});
    			
    			}
    	});
    	
    	$('#insertForm').validate({
            rules: {
            	aaCode:{
            		required:true
            	},
            	mdrFname: {
                    maxlength: 15,
                    required: true
                },
                mdrNmane: {
                    maxlength: 15,
                    required: true
                },
                mdrLname: {
                    maxlength: 15,
                    required: true
                },
                mdrAddress: {
                    required: true
                },
                mdrCity: {
                    required: true,
                },
                mdrState: {
                    required: true
                },
                mdrCountry: {
                    required: true,
                },
                mdrPin: {
                    required: true,
                },
                mdrCountry: {
                    required: true,
                },
                mdrEmail: {
                    required: true,
                },
                mdrCurrentSchoolCode: {
                    required: true,
                }
                
            },
            highlight: function(element) {
                $(element).closest('.form-group').addClass('has-error');
            },
            unhighlight: function(element) {
                $(element).closest('.form-group').removeClass('has-error');
            },
            errorElement: 'span',
            errorClass: 'help-block',
            errorPlacement: function(error, element) {
                if(element.parent('.input-group').length) {
                    error.insertAfter(element.parent());
                } else {
                    error.insertAfter(element);
                }
            }
        });
    	
    	$('#updateForm').validate({
            rules: {
            	aaCode:{
            		required:true
            	},
            	mdrFname: {
                    maxlength: 15,
                    required: true
                },
                mdrNmane: {
                    maxlength: 15,
                    required: true
                },
                mdrLname: {
                    maxlength: 15,
                    required: true
                },
                mdrAddress: {
                    required: true
                },
                mdrCity: {
                    required: true,
                },
                mdrState: {
                    required: true
                },
                mdrCountry: {
                    required: true,
                },
                mdrPin: {
                    required: true,
                },
                mdrCountry: {
                    required: true,
                },
                mdrEmail: {
                    required: true,
                },
                mdrCurrentSchoolCode: {
                    required: true,
                }
            },
            highlight: function(element) {
                $(element).closest('.form-group').addClass('has-error');
            },
            unhighlight: function(element) {
                $(element).closest('.form-group').removeClass('has-error');
            },
            errorElement: 'span',
            errorClass: 'help-block',
            errorPlacement: function(error, element) {
                if(element.parent('.input-group').length) {
                    error.insertAfter(element.parent());
                } else {
                    error.insertAfter(element);
                }
            }
    	});

    });	
    
    
    
    $('#deleteSelectedRows').click(function() {
    	var deleteSelectedRowsObj={};
    	deleteSelectedRowsObj.selectedSections=[];

    	$("input:checkbox").each(function(){
    	    var $this = $(this);

    	    if($this.is(":checked")){
    	    	var curRowId = $this.closest('tr');
    	    	deleteSelectedRowsObj.selectedSections.push(tableRowToObj(curRowId));
    	    } 
    	});
    	
    $('#confirmDelete').click(function() {
    	$.ajax({
       		url : 'http://localhost:8080/easyskool/rest/moderatorservice/deleteSelectedModeratorDetails',
   			type : 'POST',
   			data : '{"user":"aa","whereClause" : [' +deleteSelectedRowsObj.selectedSections.join(",")+']}',
   			dataType : 'json',
   			contentType : "application/json; charset=utf-8",
   			success : function(data,testStatus,response) {
   				var json = jQuery.parseJSON(response.responseText);
   				if(json.del == "success"){
   					window.location = "http://localhost:8080/easyskool/WebPages/Masters/MasterSectionHome.jsp";
   				} else	{
				alert("Error: ! "+ jqXHR.responseText);
   				}
   			}
       	});
    	$("#myConfirmModal").modal('hide');
	});
    
    $('#cancelDelete').click(function() {
    	$("#myConfirmModal").modal('hide');
	});
    
    });
    
    $('.form-fields').on('click', '#remove', function(){
    	
    	var curRowId = $(this).closest('tr');
    	$("#myConfirmModal").modal('show');
    	
    	$('#confirmDelete').click(function() {
    		$("#myConfirmModal").modal('hide');
           
       	
            var row = $(this).closest('tr');
           	$.ajax({
           		url : 'http://localhost:8080/easyskool/rest/moderatorservice/deleteModeratorsDetail',
       			type : 'POST',
       			data : '{"user":"aa","whereClause" :' + tableRowToObj(curRowId) + '}',
       			dataType : 'json',
       			contentType : "application/json; charset=utf-8",
       			success : function(data,testStatus,response) {
       				var json = jQuery.parseJSON(response.responseText);
       				if(json.del == "success"){
       					row.remove();
       					window.location = "http://localhost:8080/easyskool/WebPages/Masters/MasterSectionHome.jsp";
       				} else	{
    				alert("Error: ! "+ jqXHR.responseText);
       				}
       			}
           	});
           	
            
        });
    	
    	$('#cancelDelete').click(function() {
    		$("#myConfirmModal").modal('hide');
    	
    	});
    	
    });
  
    $('.form-fields').on('click', '#update', function(){
    	var curRowId = $(this).closest('tr').attr("id");
        $('input[id=uaaCode]')	.val		( $('#' + curRowId + ' td.aaCode').text() );
        $('input[id=umdrFname]')	.val		( $('#' + curRowId + ' td.mdrFname').text() );
        $('input[id=umdrNmane]')	.val		( $('#' + curRowId + ' td.mdrNmane').text() );
        $('input[id=umdrLname]')	.val		( $('#' + curRowId + ' td.mdrLname').text() );
        $('input[id=umdrAddress]').val	( $('#' + curRowId + ' td.mdrAddress').text() );
        $('input[id=umdrCity]')	.val		( $('#' + curRowId + ' td.mdrCity').text() );
        $('input[id=umdrState]')	.val		( $('#' + curRowId + ' td.mdrState').text() );
        $('input[id=umdrCountry]').val		( $('#' + curRowId + ' td.mdrCountry').text() );
        $('input[id=umdrPin]')	.val		( $('#' + curRowId + ' td.mdrPin').text() );
        $('input[id=umdrEmail]')	.val		( $('#' + curRowId + ' td.mdrEmail').text() );
        $('input[id=umdrCurrentSchoolCode]').val		( $('#' + curRowId + ' td.mdrCurrentSchoolCode').text() );
        $('input[id=ucreatedBy]').val	( $('#' + curRowId + ' td.createdBy').text() );
        $('input[id=ucreatedDate]').val	( $('#' + curRowId + ' td.createdDate').text() );
        $('input[id=umodifiedBy]').val	( $('#' + curRowId + ' td.modifiedBy').text() );
        $('input[id=umodifiedDate]').val	( $('#' + curRowId + ' td.modifiedDate').text() );
   });
   
   $('#updateForm').on('click', '#updateModeratorDetails', function(){
	   if($('#updateForm').valid()) {
   		formData = ($('#updateForm').serializeArray());
   				
   				$.ajax({
   					url : 'http://localhost:8080/easyskool/rest/moderatorservice/updateModeratorDetails',
						type : 'POST',
						data : '{"user":"sumitupdate","whereClause" : {"sectionId" : '+ null +'},"updateClause":'+ converter(formData)+ '}',
						dataType : 'json',
						contentType : "application/json; charset=utf-8",
						success : function(data,testStatus,response) {
							var json = jQuery.parseJSON(response.responseText);
							alert(json);
   							if(json.update == "success"){
   								window.location = "http://localhost:8080/easyskool/WebPages/Masters/MasterSectionHome.jsp";
   							} else{
   								alert("Error: ! "+ jqXHR.responseText);
   							}
						}
   					});
	   			}
        });
    
        
    $("#insertModeratorDetails").click(function(){
    	alert("hi");
    	if($('#insertForm').valid()) {
		formData = ($('#insertForm').serializeArray());
		alert(formData);
		$.ajax({
		    url : 'http://localhost:8080/easyskool/rest/moderatorservice/insertModeratorDetails',
			type : 'POST',
			data : '{"user":"sumit","fields":'+ converter(formData)+ '}',
			dataType : 'json',
			contentType : "application/json; charset=utf-8",
			success : function(data,testStatus,jqXHR) {
				var json = jQuery.parseJSON(jqXHR.responseText);
					if(json.create == "success"){
						window.location = "http://localhost:8080/easyskool/WebPages/Masters/MasterSectionHome.jsp";
					} else	{
						alert("Error: ! "+ jqXHR.responseText);
					}
			}
	    });
    	}
	});
    
    $('.form-fields').on('click', '#chkall', function(){
		if(this.checked)
			{
				$('input[name=checkboxselect]').prop('checked', true);
			} else {
				$('input[name=checkboxselect]').prop('checked', false);
			}
	});
    